﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Media.Media3D;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace UserControlLib
{
    /// <summary>
    /// SpotlightTextbBlock.xaml 的交互逻辑
    /// </summary>
    public partial class SpotlightTextbBlock : UserControl
    {
        public SpotlightTextbBlock()
        {
            InitializeComponent();
        }

        // 定义 Text 依赖属性
        public string Text
        {
            get { return (string)GetValue(TextProperty); }
            set { SetValue(TextProperty, value); }
        }

        public static readonly DependencyProperty TextProperty =
            DependencyProperty.Register("Text", typeof(string), typeof(SpotlightTextbBlock), new PropertyMetadata("SpotlightTextbBlock"));

        // 定义 FontSize 依赖属性
        public double FontSize
        {
            get { return (double)GetValue(FontSizeProperty); }
            set { SetValue(FontSizeProperty, value); }
        }

        public static readonly DependencyProperty FontSizeProperty =
            DependencyProperty.Register("FontSize", typeof(double), typeof(SpotlightTextbBlock), new PropertyMetadata(48.0));


        public override void OnApplyTemplate()
        {
            // 设置 EllipseGeometry 的初始大小和位置
            PART_EllipseGeometry.RadiusX = FontSize / 2;
            PART_EllipseGeometry.RadiusY = FontSize / 2;
            PART_EllipseGeometry.Center = new Point(0, FontSize / 2);

            // 确保动画在 TextBlock 加载后启动
            PART_TextBlockTop.Loaded += _textBlockTop_Loaded;
        }


        private void _textBlockTop_Loaded(object sender, RoutedEventArgs e)
        {
            // 动画：改变 TranslateTransform.X 属性（移动聚光灯）
            DoubleAnimation translateAnimation = new DoubleAnimation
            {
                From = -PART_TextBlockTop.ActualWidth,
                To = PART_TextBlockTop.ActualWidth,
                Duration = TimeSpan.FromSeconds(5),
                RepeatBehavior = RepeatBehavior.Forever,
                AutoReverse = true
            };

            // 将动画应用到 TranslateTransform.X 属性
            Storyboard.SetTarget(translateAnimation, PART_EllipseGeometry.Transform);
            Storyboard.SetTargetProperty(translateAnimation, new PropertyPath("(TranslateTransform.X)"));

            // 创建并开始 Storyboard
            var storyboard = new Storyboard();
            storyboard.Children.Add(translateAnimation);
            storyboard.Begin(this);
        }
    }
}
